package com.rgbgame.game;

import processing.core.PApplet;
import processing.core.PImage;

/**
 * @author Will
 * @date Mar 4, 2008 - 2:13:42 PM
 */
public class Sprite {

    private PImage[] images;
    private int numImages;
    private int frame; // current frame
    private float h; // height at unit scaling
    private float w; // width at unit scaling
    private Vector3d position; // center position of sprite
    // private float rotation; //no rotation for now
    private float scaling;

    public Sprite(PImage[] _images, Vector3d _position, float _w, float _h,
                  float _scaling) {
        images = _images;
        position = new Vector3d(_position); // or should we prefer doing
        // this by reference?
        w = _w;
        h = _h;
        scaling = _scaling;
        numImages = images.length;
        frame = 0;
    }

    public Sprite(PImage[] _images, Vector3d _position, float _scaling) {
        this(_images, _position, _images[0].width, _images[0].height,
                _scaling);
    }

    public Sprite(PImage[] _images, Vector3d _position) {
        this(_images, _position, _images[0].width, _images[0].height, 1.0f);
    }

    public Sprite(PImage[] _images, Vector3d _position, float _w, float _h) {
        this(_images, _position, _w, _h, 1.0f);
    }

    public Sprite(PImage _image, Vector3d _position, float _w, float _h,
                  float _scaling) {
        PImage[] _images = new PImage[1];
        _images[0] = _image;
        set(_images, _position, _w, _h, _scaling);
    }

    public Sprite(PImage _image, Vector3d _position, float _scaling) {
        PImage[] _images = new PImage[1];
        _images[0] = _image;
        set(_images, _position, _images[0].width, _images[0].height,
                _scaling);
    }

    public Sprite(PImage _image, Vector3d _position) {
        PImage[] _images = new PImage[1];
        _images[0] = _image;
        set(_images, _position, _images[0].width, _images[0].height, 1.0f);
    }

    public Sprite(PImage _image, Vector3d _position, float _w, float _h) {
        PImage[] _images = new PImage[1];
        _images[0] = _image;
        set(_images, _position, _w, _h, 1.0f);
    }

    public void set(PImage[] _images, Vector3d _position, float _w,
                    float _h, float _scaling) {
        images = _images;
        position = new Vector3d(_position); // or should we prefer doing
        // this by reference?
        w = _w;
        h = _h;
        scaling = _scaling;
        numImages = images.length;
        frame = 0;
    }

    // draws and steps forward image
    public void draw(PApplet p) {
        p.image(images[frame], position.x - scaling * w / 2.0f, position.y
                - scaling * h / 2.0f, scaling * w, scaling * h); // no
        // scaling
        // yet
        // println(position.x+"; "+position.y+"; "+frame);
        incrementFrame();
    }

    private void incrementFrame() {
        frame++;
        if (frame == numImages)
            frame = 0;
    }

    public int getFrame() {
        return frame;
    }

    public void setFrame(int f) {
        frame = f;
        if (f < 0)
            f = 0;
        if (f > numImages)
            f = numImages - 1;
    }

    public float getScaling() {
        return scaling;
    }

    public void setScaling(float s) {
        scaling = s;
    }


}
